CKEDITOR.dialog.add("simple-image-browser-dialog", function (editor) {
    return {
        title: editor.lang.simpleimagebrowser.title,
        minWidth: 800,
        minHeight: 400,
        maxWidth: 800,
        maxHeight: 400,
        contents: [{
            id: "tab-step1",
            label: editor.lang.simpleimagebrowser.label,
            elements: [{
                type: "html",
                align: "left",
                style: "font-size: 20px; font-weight: bold;",
                html: editor.lang.simpleimagebrowser.fileinput
            }, {
                type: "html",
                align: "left",
                id: "uploadimg",
                html: '<input type="file" style="display: inline-block; max-width: 280px;" class="cke_dialog_ui_input_text" name="upload-img-input" accept="image/*" multiple id="upload-img-input" /><a id="upload-img-btn" style="margin-left: 10px" hidefocus="true" class="cke_dialog_ui_button" role="button"><span class="cke_dialog_ui_button">' + editor.lang.simpleimagebrowser.upload + '</span></a><div id="upload-status" style="display:none">' + editor.lang.simpleimagebrowser.uploading + '</div>'
            }, {
                type: "html",
                align: "left",
                id: "titleid",
                style: "font-size: 20px; font-weight: bold;",
                html: editor.lang.simpleimagebrowser.label
            }, {
                type: "html",
                align: "left",
                id: "msg",
                style: "",
                html: '<div id="imageBrowser"></div>'
            }]
        }, {
            id: "tab-step2",
            label: "About this plugin",
            elements: [{
                type: "html",
                align: "left",
                id: "framepreviewtitleid",
                style: "font-size: 20px; font-weight: bold;",
                html: "About this author"
            }, {
                type: "html",
                align: "left",
                id: "descriptionid",
                style: "position:relative;width:800px;",
                html: 'EpicSoftworks released this plugin for free under the MIT licence.<br />You are free to use this for personal, educational or commercial use.<br /><br />Free as in, the freedom to use.<br /><br /><a href="http://epicsoftworks.nl/" target="_blank">Visit my website >></a>'
            }]
        }]
    }
}), CKEDITOR.plugins.add("simpleimagebrowser", {
    lang: 'en,vi',
    init: function (a) {

        $(document.body).on('click', '#upload-img-btn', function (e) {
            if (document.simpleImageBrowserUploading)
                return;

            e.preventDefault();

            var inputFiles = $('#upload-img-input');
            var uploadBtn = $('#upload-img-btn');
            var uploadStatus = $('#upload-status');

            var files = $('#upload-img-input').get(0).files;

            if (files.length === 0)
                return;

            var data = new FormData();
            for (var x = 0; x < files.length; x++) {
                data.append("file" + x, files[x]);
            }
            
            $.ajax(CKEDITOR.config.simpleImageBrowserUploadActionURL, {
                type: "POST",
                data: data,
                contentType: false,
                processData: false,
                beforeSend: function () {
                    inputFiles.prop('disabled', true);
                    uploadBtn.prop('disabled', true);
                    uploadStatus.show();
                    document.simpleImageBrowserUploading = true;
                },
                success: function (result) {
                    inputFiles.prop('disabled', false);
                    uploadBtn.prop('disabled', false);
                    uploadStatus.hide();
                    document.simpleImageBrowserUploading = false;

                    switch (result.status) {
                        case 200:
                            var obj = $.parseJSON(result.object);
                            $.each(obj, function (idx, img) {
                                $("#imageBrowser").append("<div onclick=\"CKEDITOR.tools.simpleimagebrowserinsertpicture('" + img + "');\" style=\"position:relative;width:75px;height:75px;margin:5px;background-image:url('" + img + "');background-repeat:no-repeat;background-size:125%;background-position:center center;float:left;\"></div>");
                            });
                            break;
                        default:
                            alert(result.message);
                            break;
                    }
                }
            });
        });

        "undefined" == typeof CKEDITOR.config.simpleImageBrowserListType && (CKEDITOR.config.simpleImageBrowserListType = "thumbnails"), a.on("dialogShow", function (a) {
            var b;
            b = a.data, "simple-image-browser-dialog" === b.getName() && $.get(CKEDITOR.config.simpleImageBrowserURL, function (a) {
                var b;
                return console.log(a), b = $.parseJSON(a), a = "", $.each(b, function (b, c) {
                    a = "thumbnails" === CKEDITOR.config.simpleImageBrowserListType ? a + "<div onclick=\"CKEDITOR.tools.simpleimagebrowserinsertpicture('" + c.url + "');\" style=\"position:relative;width:75px;height:75px;margin:5px;background-image:url('" + c.url + "');background-repeat:no-repeat;background-size:125%;background-position:center center;float:left;\"></div>" : "link"
                }), $("#imageBrowser").html(a)
            })
        }), a.addCommand("simple-image-browser-start", new CKEDITOR.dialogCommand("simple-image-browser-dialog")), CKEDITOR.tools.simpleimagebrowserinsertpicture = function (b) {
            var c, d;
            console.log(b), a = CKEDITOR.currentInstance, c = CKEDITOR.dialog.getCurrent(), d = '<img src="' + b + '" data-cke-saved-src="' + b + '" />', a.config.allowedContent = !0, a.insertHtml(d.trim()), c.hide()
        }, a.ui.addButton("Simple Image Browser", {
            label: a.lang.simpleimagebrowser.title,
            command: "simple-image-browser-start",
            icon: this.path + "images/icon.png"
        })
    }
});